<%@page import="java.net.URLDecoder"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.logistics.distribution.Inoutstoreroom"%>
<%@page import="com.logistics.database.Distributiondatabase"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Getinoutbyquery</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	<%
  		 //System.out.println("begin!");
  		 request.setCharacterEncoding("utf-8");
  		 response.setContentType("text/xml;charset=UTF-8");
	     response.setHeader("Cache-Control", "no-cache");
	     String storeroom = request.getParameter("storeroom");
	     if(storeroom==null)
         	storeroom="";
         	
        // System.out.println("storeroom:"+storeroom);
         
         String name = request.getParameter("name");
         if(name==null)
         	name="";
         	
         //System.out.println("name:"+name);
         
         String begin_time_string = request.getParameter("begin_time");
         if(begin_time_string==null)
         	begin_time_string="";
         	
         //System.out.println("begin_time:"+begin_time_string);
         
           String end_time_string = request.getParameter("end_time");
         if(end_time_string==null)
         	end_time_string="";
         	
         //System.out.println("end_time:"+end_time_string);
         
         Distributiondatabase distributiondatabase =new Distributiondatabase();
         ArrayList<Inoutstoreroom> inoutstorerooms=new ArrayList<Inoutstoreroom>();
         ResultSet resultset=distributiondatabase.getInoutStoreroom();
         while(resultset.next())
         {
         	int Inout_id=resultset.getInt("inout_id");
         	//获取仓库名
         	//System.out.println("!");
         	int storeroom_id=resultset.getInt("storeroom_id");
         	//System.out.println("!!");
         	
         	Distributiondatabase distributiondatabase2=new Distributiondatabase();
         	ResultSet tempreSet=distributiondatabase2.getStoreroom("where storeroom_id=\'"+storeroom_id+"\'");
         	tempreSet.next();
			String Storeroom=tempreSet.getString("storeroom_name");
			//System.out.println("storeroom is "+Storeroom);
			//System.out.println("!!!");
			//System.out.println(resultset.getInt("inout_id"));
			//System.out.println("!!!");
			int Product_id = resultset.getInt("product_id");
			//System.out.println(Product_id);
			//获取商品名和其价格。
			tempreSet=distributiondatabase2.productsQuery(Product_id);
			tempreSet.next();
			String Product_name = tempreSet.getString("product_name");
			//System.out.println("!!!!!");
			double Price = tempreSet.getDouble("price");
			//System.out.println("!!!!!!");
			int Product_number = resultset.getInt("product_number");
			//System.out.println("!!!!!!!");
			double Total_price=Price*Product_number;
			//计算总价
			
			java.sql.Date Inoutdate=resultset.getDate("inoutdate");
			//System.out.println("!!!!!!!!");
			String Inout_type = resultset.getString("inout_type");
      		Inoutstoreroom inoutstoreroom=new Inoutstoreroom(Inout_id, Storeroom, Product_id, Product_name, Price, Product_number, Total_price, Inoutdate,Inout_type);
      		inoutstorerooms.add(inoutstoreroom);
         }
        // System.out.println(inoutstorerooms.size());
         if(!storeroom.equals(""))
         {
         	//System.out.println("storeroom_search:"+storeroom);
         	for(int i=inoutstorerooms.size()-1;i>=0;--i)
         	{
         		//System.out.println("i:"+i);
         		if(!inoutstorerooms.get(i).getStoreroom().equals(storeroom))
         		{
         			inoutstorerooms.remove(i);
         		}
         	}
         }
         if(!"".equals(name))
         {
         	//System.out.println("name_search:"+name);
         	for(int i=inoutstorerooms.size()-1;i>=0;--i)
         	{
         		if(!inoutstorerooms.get(i).getProduct_name().equals(name))
         		{
         			inoutstorerooms.remove(i);
         		}
         	}	
         }
         if((!"".equals(begin_time_string))&&(!"".equals(end_time_string)))
         {
         	//System.out.println("date_search!");
         	java.util.Date tempdate=new java.util.Date();
         	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         	tempdate=format.parse(begin_time_string);
         	java.sql.Date begin_time=new java.sql.Date(tempdate.getTime());
         	tempdate=format.parse(end_time_string);
         	java.sql.Date end_time=new java.sql.Date(tempdate.getTime());
         	for(int i=inoutstorerooms.size()-1;i>=0;--i)
         	{
         		java.sql.Date tempinoutdate= inoutstorerooms.get(i).getInoutdate();
         		if(tempinoutdate.getTime()>end_time.getTime()||tempinoutdate.getTime()<begin_time.getTime())
         		{
         			inoutstorerooms.remove(i);
         		}
         	}
         }
         String xml_start = "<inoutstorerooms>";
    	 String xml_end = "</inoutstorerooms>";
    	 String xml="";
    	 for(int i=0;i<inoutstorerooms.size();++i)
    	 {
    	 	xml+="<inoutstoreroom><inout_id>"+inoutstorerooms.get(i).getInout_id()+"</inout_id><storeroom>"+inoutstorerooms.get(i).getStoreroom()+"</storeroom><product_id>"+inoutstorerooms.get(i).getProduct_id()+"</product_id><product_name>"+inoutstorerooms.get(i).getProduct_name()+"</product_name><price>"+inoutstorerooms.get(i).getPrice()+"</price><product_number>"+inoutstorerooms.get(i).getProduct_number()+"</product_number><total_price>"+inoutstorerooms.get(i).getTotal_price()+"</total_price><inoutdate>"+inoutstorerooms.get(i).getInoutdate().toString()+"</inoutdate><inouttype>"+inoutstorerooms.get(i).getInout_type()+"</inouttype></inoutstoreroom>";	
    	 }
    	String last_xml=xml_start+xml+xml_end;
    	//System.out.println(last_xml);
    	response.getWriter().write(last_xml);
    	response.getWriter().close();
  	 %>
  </body>
</html>
